####
# Find Clang compiler and tools to use
####
if(LLVMLIB_FRONTEND AND NOT CROSS_COMPILE AND NOT SPIRV_CLANG_FOUND)
	if(NOT CLANG_FOUND)
		find_program(CLANG_FOUND clang NAMES clang clang-3.9 clang-4.0 clang-5.0 clang-6.0 clang-7 clang-8 clang-9)
	endif()
	if (NOT OPT_FOUND)
		find_program(OPT_FOUND opt NAMES opt opt-3.9 opt-4.0 opt-5.0 opt-6.0 opt-7 opt-8 opt-9)
	endif()
	if(NOT LLVM_LINK_FOUND)
		find_program(LLVM_LINK_FOUND llvm-link NAMES llvm-link llvm-link-3.9 llvm-link-4.0 llvm-link-5.0 llvm-link-6.0 llvm-link-7 llvm-link-8 llvm-link-9)
	endif()
	if(CLANG_FOUND)
		message(STATUS "CLang compiler found: " ${CLANG_FOUND})
		# check whether CLang is installed in the version 3.9 or greater
		# https://stackoverflow.com/questions/16150888/cmake-branch-on-clang-version
		EXECUTE_PROCESS( COMMAND ${CLANG_FOUND} --version OUTPUT_VARIABLE clang_full_version_string )
		string (REGEX REPLACE ".*clang version ([0-9]+\\.[0-9]+).*" "\\1" CLANG_VERSION_STRING ${clang_full_version_string})
		if (CLANG_VERSION_STRING VERSION_LESS 3.9)
			message(SEND_ERROR "CLang version ${CLANG_VERSION_STRING} is not supported, update your CLang to version 3.9+!")
		else()
			message(STATUS "Using enhanced OpenCL features of Clang 3.9+!")
			set(ADVANCED_OPENCL_FEATURES ON)
		endif()
	else()
		message(WARNING "No CLang compiler found!")
	endif()
	if(OPT_FOUND)
		message(STATUS "Clang opt found: " ${OPT_FOUND})
	endif()
	if(LLVM_LINK_FOUND)
		message(STATUS "LLVM-link found: " ${LLVM_LINK_FOUND})
	endif()
endif()

####
# Find libClang when front-end is enabled
####
if(CLANG_LIBRARY)
	if(NOT LIBCLANG_LIBRARIES)
		find_library(LIBCLANG_LIBRARY_FRONTEND NAMES clangFrontend libclangFrontend)
		find_library(LIBCLANG_LIBRARY_CODEGEN NAMES clangCodeGen libclangCodeGen)

		if(LIBCLANG_LIBRARY_FRONTEND AND LIBCLANG_LIBRARY_CODEGEN)
			set(LIBCLANG_LIBRARIES ${LIBCLANG_LIBRARY_FRONTEND} ${LIBCLANG_LIBRARY_CODEGEN})
		endif()
	endif()
	if(NOT LIBCLANG_INCLUDE_PATH)
		find_path(LIBCLANG_INCLUDE_PATH clang-c/Index.h)
	endif()
	if(LIBCLANG_LIBRARIES AND LIBCLANG_INCLUDE_PATH)
		message(STATUS "Using LibClang libraries: ${LIBCLANG_LIBRARIES}")
		message(STATUS "Using LibClang headers: ${LIBCLANG_INCLUDE_PATH}")
		set(VC4C_ENABLE_LIBCLANG ON)
	else()
		message(WARNING "LibClang enabled, but no libClang libraries were found")
		set(VC4C_ENABLE_LIBCLANG OFF)
	endif()
endif()